<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
































<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=6.6.0" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.6.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg?v=6.6.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg?v=6.6.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.6.0" color="#222">









<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.6.0',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="上一篇文章中介绍了 Hikari 的并发管理（包含了连接的获取、释放、添加），这篇主要介绍一下 DataSource 等各种 JDBC 接口的实现，以及各个类之间的关系。 HikariDataSourceHikariDataSource 这个类是 Hikari 直接暴露给用户的类，实现了 DataSource 接口。 老规矩先看几个重要的属性1234567891011// 注意继承关系public">
<meta name="keywords" content="源码分析,HikariCP,并发">
<meta property="og:type" content="article">
<meta property="og:title" content="HikariCP源码深度解读（二）">
<meta property="og:url" content="http://yoursite.com/2019/03/26/HikariCP源码深度解读（二）/index.html">
<meta property="og:site_name" content="isunimp">
<meta property="og:description" content="上一篇文章中介绍了 Hikari 的并发管理（包含了连接的获取、释放、添加），这篇主要介绍一下 DataSource 等各种 JDBC 接口的实现，以及各个类之间的关系。 HikariDataSourceHikariDataSource 这个类是 Hikari 直接暴露给用户的类，实现了 DataSource 接口。 老规矩先看几个重要的属性1234567891011// 注意继承关系public">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-04-11T13:50:16.667Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HikariCP源码深度解读（二）">
<meta name="twitter:description" content="上一篇文章中介绍了 Hikari 的并发管理（包含了连接的获取、释放、添加），这篇主要介绍一下 DataSource 等各种 JDBC 接口的实现，以及各个类之间的关系。 HikariDataSourceHikariDataSource 这个类是 Hikari 直接暴露给用户的类，实现了 DataSource 接口。 老规矩先看几个重要的属性1234567891011// 注意继承关系public">






  <link rel="canonical" href="http://yoursite.com/2019/03/26/HikariCP源码深度解读（二）/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>HikariCP源码深度解读（二） | isunimp</title>
  












  <noscript>
  <style>
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion .logo-line-before i { left: initial; }
    .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">isunimp</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">isunimp</p>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/26/HikariCP源码深度解读（二）/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="isunimp">
      <meta itemprop="description" content="涉猎广泛，不亦乐乎，不求甚解。">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="isunimp">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">HikariCP源码深度解读（二）

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-03-26 16:45:20" itemprop="dateCreated datePublished" datetime="2019-03-26T16:45:20+08:00">2019-03-26</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-04-11 21:50:16" itemprop="dateModified" datetime="2019-04-11T21:50:16+08:00">2019-04-11</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/HikariCP/" itemprop="url" rel="index"><span itemprop="name">HikariCP</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><a href="https://isunimp.gitee.io/2019/03/19/HikariCP%E6%BA%90%E7%A0%81%E6%B7%B1%E5%BA%A6%E8%A7%A3%E8%AF%BB%EF%BC%88%E4%B8%80%EF%BC%89/" target="_blank" rel="noopener">上一篇</a>文章中介绍了 Hikari 的并发管理（包含了连接的获取、释放、添加），这篇主要介绍一下 <code>DataSource</code> 等各种 JDBC 接口的实现，以及各个类之间的关系。</p>
<h3 id="HikariDataSource"><a href="#HikariDataSource" class="headerlink" title="HikariDataSource"></a>HikariDataSource</h3><p><a href="https://github.com/brettwooldridge/HikariCP/blob/HikariCP-3.3.1/src/main/java/com/zaxxer/hikari/HikariDataSource.java" target="_blank" rel="noopener">HikariDataSource </a>这个类是 <code>Hikari</code> 直接暴露给用户的类，实现了 <code>DataSource</code> 接口。</p>
<p>老规矩先看几个重要的属性<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 注意继承关系</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HikariDataSource</span> <span class="keyword">extends</span> <span class="title">HikariConfig</span> <span class="keyword">implements</span> <span class="title">DataSource</span>, <span class="title">Closeable</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// 原子布尔型变量标记连接池状态</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> AtomicBoolean isShutdown = <span class="keyword">new</span> AtomicBoolean();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 连接池对象</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> HikariPool fastPathPool;</span><br><span class="line">    <span class="comment">// 连接池对象</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">volatile</span> HikariPool pool;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>为什么有两个连接池对象？</p>
<p>先看看两个构造方法<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 构造器 1</span></span><br><span class="line"><span class="comment">// 未进行任务初始化</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">HikariDataSource</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">super</span>();</span><br><span class="line">    fastPathPool = <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 构造器 2</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">HikariDataSource</span><span class="params">(HikariConfig configuration)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 校验配置是否有效</span></span><br><span class="line">   configuration.validate();</span><br><span class="line">   <span class="comment">// 值拷贝，HikariDataSource 继承自 HikariConfig</span></span><br><span class="line">   configuration.copyStateTo(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">   LOGGER.info(<span class="string">"&#123;&#125; - Starting..."</span>, configuration.getPoolName());</span><br><span class="line">   <span class="comment">// 初始化连接池</span></span><br><span class="line">   pool = fastPathPool = <span class="keyword">new</span> HikariPool(<span class="keyword">this</span>);</span><br><span class="line">   LOGGER.info(<span class="string">"&#123;&#125; - Start completed."</span>, configuration.getPoolName());</span><br><span class="line"></span><br><span class="line">   <span class="keyword">this</span>.seal();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>再看 <code>getConnection</code> 方法实现</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Connection <span class="title">getConnection</span><span class="params">()</span> <span class="keyword">throws</span> SQLException</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (isClosed()) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> SQLException(<span class="string">"HikariDataSource "</span> + <span class="keyword">this</span> + <span class="string">" has been closed."</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// fastPathPool 不为空，直接使用</span></span><br><span class="line">    <span class="keyword">if</span> (fastPathPool != <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> fastPathPool.getConnection();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    HikariPool result = pool;</span><br><span class="line">    <span class="keyword">if</span> (result == <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">synchronized</span> (<span class="keyword">this</span>) &#123;</span><br><span class="line">            result = pool;</span><br><span class="line">            <span class="comment">// double check</span></span><br><span class="line">            <span class="keyword">if</span> (result == <span class="keyword">null</span>) &#123;</span><br><span class="line">                validate();</span><br><span class="line">                LOGGER.info(<span class="string">"&#123;&#125; - Starting..."</span>, getPoolName());</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="comment">// 初始化 pool</span></span><br><span class="line">                    pool = result = <span class="keyword">new</span> HikariPool(<span class="keyword">this</span>);</span><br><span class="line">                    <span class="keyword">this</span>.seal();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">catch</span> (PoolInitializationException pie) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (pie.getCause() <span class="keyword">instanceof</span> SQLException) &#123;</span><br><span class="line">                        <span class="keyword">throw</span> (SQLException) pie.getCause();</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="keyword">throw</span> pie;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                LOGGER.info(<span class="string">"&#123;&#125; - Start completed."</span>, getPoolName());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> result.getConnection();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当使用第二个构造器初始化时，会初始化好 fastPathPool，getConnection 方法可以直接使用。<br>当使用第一个构造方法初始化，会等到 getConnection 方法才会去初始化 pool，为了保证线程安全使用 double check的方式，pool 变量还加了 volatile 关键字，效率必然没有第二种初始化的方式高，可能这就是作者为什么声明了两个 pool 的原因吧， 效率高的命名为 fastPathPool。</p>
<h3 id="HikariPool"><a href="#HikariPool" class="headerlink" title="HikariPool"></a>HikariPool</h3><p>HikariDataSource 只是用户接口，真正提供连接池服务的是 <a href="https://github.com/brettwooldridge/HikariCP/blob/86cabf08623a8f9c9ab697a95a277690496b6fae/src/main/java/com/zaxxer/hikari/pool/HikariPool.java" target="_blank" rel="noopener">HikariPool</a>，还是先看属性：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">HikariPool</span> <span class="keyword">extends</span> <span class="title">PoolBase</span> <span class="keyword">implements</span> <span class="title">HikariPoolMXBean</span>, <span class="title">IBagStateListener</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> POOL_NORMAL = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> POOL_SUSPENDED = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> POOL_SHUTDOWN = <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">volatile</span> <span class="keyword">int</span> poolState;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">long</span> aliveBypassWindowMs = Long.getLong(<span class="string">"com.zaxxer.hikari.aliveBypassWindowMs"</span>, MILLISECONDS.toMillis(<span class="number">500</span>));</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">long</span> housekeepingPeriodMs = Long.getLong(<span class="string">"com.zaxxer.hikari.housekeeping.periodMs"</span>, SECONDS.toMillis(<span class="number">30</span>));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String EVICTED_CONNECTION_MESSAGE = <span class="string">"(connection was evicted)"</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String DEAD_CONNECTION_MESSAGE = <span class="string">"(connection is dead)"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建连接的任务，实现了Callable</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> PoolEntryCreator poolEntryCreator = <span class="keyword">new</span> PoolEntryCreator(<span class="keyword">null</span> <span class="comment">/*logging prefix*/</span>);</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> PoolEntryCreator postFillPoolEntryCreator = <span class="keyword">new</span> PoolEntryCreator(<span class="string">"After adding "</span>);</span><br><span class="line">    <span class="comment">// 任务队列副本</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Collection&lt;Runnable&gt; addConnectionQueue;</span><br><span class="line">    <span class="comment">// 执行添加任务的线程池</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> ThreadPoolExecutor addConnectionExecutor;</span><br><span class="line">    <span class="comment">// 执行关闭连接的线程池</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> ThreadPoolExecutor closeConnectionExecutor;</span><br><span class="line">    <span class="comment">// 并发管理组件</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> ConcurrentBag&lt;PoolEntry&gt; connectionBag;</span><br><span class="line">    <span class="comment">// 连接泄露检查相关</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> ProxyLeakTaskFactory leakTaskFactory;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> SuspendResumeLock suspendResumeLock;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定时调度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> ScheduledExecutorService houseKeepingExecutorService;</span><br><span class="line">    <span class="comment">// 定时任务（关闭空闲的连接）</span></span><br><span class="line">    <span class="keyword">private</span> ScheduledFuture&lt;?&gt; houseKeeperTask;</span><br></pre></td></tr></table></figure>
<p>构造方法大致看一下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">HikariPool</span><span class="params">(<span class="keyword">final</span> HikariConfig config)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 主要初始化一些参数</span></span><br><span class="line">    <span class="keyword">super</span>(config);</span><br><span class="line">    <span class="comment">// 并发管理组件，HikariPool实现了 IBagStateListener 接口</span></span><br><span class="line">    <span class="keyword">this</span>.connectionBag = <span class="keyword">new</span> ConcurrentBag&lt;&gt;(<span class="keyword">this</span>);</span><br><span class="line">    <span class="comment">// 这个和 JMX 有关，不管</span></span><br><span class="line">    <span class="keyword">this</span>.suspendResumeLock = config.isAllowPoolSuspension() ? <span class="keyword">new</span> SuspendResumeLock() : SuspendResumeLock.FAUX_LOCK;</span><br><span class="line">    <span class="comment">// 参数化定时调度</span></span><br><span class="line">    <span class="keyword">this</span>.houseKeepingExecutorService = initializeHouseKeepingExecutorService();</span><br><span class="line">    <span class="comment">// 检查数据库配置是否有效</span></span><br><span class="line">    checkFailFast();</span><br><span class="line">    <span class="comment">// 和Metrics有关，先不管</span></span><br><span class="line">    <span class="keyword">if</span> (config.getMetricsTrackerFactory() != <span class="keyword">null</span>) &#123;</span><br><span class="line">        setMetricsTrackerFactory(config.getMetricsTrackerFactory());</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        setMetricRegistry(config.getMetricRegistry());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    setHealthCheckRegistry(config.getHealthCheckRegistry());</span><br><span class="line"></span><br><span class="line">    handleMBeans(<span class="keyword">this</span>, <span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化线程池</span></span><br><span class="line">    ThreadFactory threadFactory = config.getThreadFactory();</span><br><span class="line">    LinkedBlockingQueue&lt;Runnable&gt; addQueue = <span class="keyword">new</span> LinkedBlockingQueue&lt;&gt;(config.getMaximumPoolSize());</span><br><span class="line">    <span class="keyword">this</span>.addConnectionQueue = unmodifiableCollection(addQueue);</span><br><span class="line">    <span class="keyword">this</span>.addConnectionExecutor = createThreadPoolExecutor(addQueue, poolName + <span class="string">" connection adder"</span>, threadFactory, <span class="keyword">new</span> ThreadPoolExecutor.DiscardPolicy());</span><br><span class="line">    <span class="keyword">this</span>.closeConnectionExecutor = createThreadPoolExecutor(config.getMaximumPoolSize(), poolName + <span class="string">" connection closer"</span>, threadFactory, <span class="keyword">new</span> ThreadPoolExecutor.CallerRunsPolicy());</span><br><span class="line">    <span class="comment">// </span></span><br><span class="line">    <span class="keyword">this</span>.leakTaskFactory = <span class="keyword">new</span> ProxyLeakTaskFactory(config.getLeakDetectionThreshold(), houseKeepingExecutorService);</span><br><span class="line">    <span class="comment">// 空闲连接监测任务，初始化池的时候已经启动了</span></span><br><span class="line">    <span class="keyword">this</span>.houseKeeperTask = houseKeepingExecutorService.scheduleWithFixedDelay(<span class="keyword">new</span> HouseKeeper(), <span class="number">100L</span>, housekeepingPeriodMs, MILLISECONDS);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (Boolean.getBoolean(<span class="string">"com.zaxxer.hikari.blockUntilFilled"</span>) &amp;&amp; config.getInitializationFailTimeout() &gt; <span class="number">1</span>) &#123;</span><br><span class="line">        addConnectionExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());</span><br><span class="line">        addConnectionExecutor.setMaximumPoolSize(Runtime.getRuntime().availableProcessors());</span><br><span class="line"></span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">long</span> startTime = currentTime();</span><br><span class="line">        <span class="keyword">while</span> (elapsedMillis(startTime) &lt; config.getInitializationFailTimeout() &amp;&amp; getTotalConnections() &lt; config.getMinimumIdle()) &#123;</span><br><span class="line">            quietlySleep(MILLISECONDS.toMillis(<span class="number">100</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        addConnectionExecutor.setCorePoolSize(<span class="number">1</span>);</span><br><span class="line">        addConnectionExecutor.setMaximumPoolSize(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="获取连接"><a href="#获取连接" class="headerlink" title="获取连接"></a>获取连接</h4><p>还是看最繁琐的 getConnection</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Connection <span class="title">getConnection</span><span class="params">(<span class="keyword">final</span> <span class="keyword">long</span> hardTimeout)</span> <span class="keyword">throws</span> SQLException</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 不管这个</span></span><br><span class="line">    suspendResumeLock.acquire();</span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">long</span> startTime = currentTime();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">long</span> timeout = hardTimeout;</span><br><span class="line">        <span class="keyword">do</span> &#123;</span><br><span class="line">            <span class="comment">// 从并发管理组件获取</span></span><br><span class="line">            PoolEntry poolEntry = connectionBag.borrow(timeout, MILLISECONDS);</span><br><span class="line">            <span class="keyword">if</span> (poolEntry == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">break</span>; <span class="comment">// We timed out... break and throw exception</span></span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">final</span> <span class="keyword">long</span> now = currentTime();</span><br><span class="line">            <span class="comment">// 检查有效性</span></span><br><span class="line">            <span class="keyword">if</span> (poolEntry.isMarkedEvicted() || (elapsedMillis(poolEntry.lastAccessed, now) &gt; aliveBypassWindowMs &amp;&amp; !isConnectionAlive(poolEntry.connection))) &#123;</span><br><span class="line">                closeConnection(poolEntry, poolEntry.isMarkedEvicted() ? EVICTED_CONNECTION_MESSAGE : DEAD_CONNECTION_MESSAGE);</span><br><span class="line">                timeout = hardTimeout - elapsedMillis(startTime);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                metricsTracker.recordBorrowStats(poolEntry, startTime);</span><br><span class="line">                <span class="comment">// 创建代理连接，返回的类型是 ProxyConnection</span></span><br><span class="line">                <span class="comment">// 同时创建一个定时调度任务，检查连接是否泄漏</span></span><br><span class="line">                <span class="keyword">return</span> poolEntry.createProxyConnection(leakTaskFactory.schedule(poolEntry), now);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">while</span> (timeout &gt; <span class="number">0L</span>);</span><br><span class="line"></span><br><span class="line">        metricsTracker.recordBorrowTimeoutStats(startTime);</span><br><span class="line">        <span class="keyword">throw</span> createTimeoutException(startTime);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">        Thread.currentThread().interrupt();</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> SQLException(poolName + <span class="string">" - Interrupted during connection acquisition"</span>, e);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">finally</span> &#123;</span><br><span class="line">        suspendResumeLock.release();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="关闭连接"><a href="#关闭连接" class="headerlink" title="关闭连接"></a>关闭连接</h4><p>我们在使用连接后会调用 <code>close</code> 方法关闭连接，但是在使用连接池的时，不会真的将连接关闭，在获取连接时，Hikari 会创建一个代理返回，所以看一下 <a href="https://github.com/brettwooldridge/HikariCP/blob/86cabf08623a8f9c9ab697a95a277690496b6fae/src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java" target="_blank" rel="noopener">ProxyConnection</a> 的 close 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">close</span><span class="params">()</span> <span class="keyword">throws</span> SQLException</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 先关闭所有 Statement</span></span><br><span class="line">    closeStatements();</span><br><span class="line">    <span class="comment">// delegate 是 jdbc 的 Connection 对象</span></span><br><span class="line">    <span class="keyword">if</span> (delegate != ClosedConnection.CLOSED_CONNECTION) &#123;</span><br><span class="line">        <span class="comment">// 取消泄漏检查任务</span></span><br><span class="line">        <span class="comment">// 非正常情况，连接长时间没有释放，泄漏检查任务就会被触发</span></span><br><span class="line">        leakTask.cancel();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (isCommitStateDirty &amp;&amp; !isAutoCommit) &#123;</span><br><span class="line">                delegate.rollback();</span><br><span class="line">                lastAccess = currentTime();</span><br><span class="line">                LOGGER.debug(<span class="string">"&#123;&#125; - Executed rollback on connection &#123;&#125; due to dirty commit state on close()."</span>, poolEntry.getPoolName(), delegate);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (dirtyBits != <span class="number">0</span>) &#123;</span><br><span class="line">                poolEntry.resetConnectionState(<span class="keyword">this</span>, dirtyBits);</span><br><span class="line">                lastAccess = currentTime();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            delegate.clearWarnings();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!poolEntry.isMarkedEvicted()) &#123;</span><br><span class="line">                <span class="keyword">throw</span> checkException(e);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">finally</span> &#123;</span><br><span class="line">            delegate = ClosedConnection.CLOSED_CONNECTION;</span><br><span class="line">            <span class="comment">// 释放连接</span></span><br><span class="line">            poolEntry.recycle(lastAccess);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="添加连接"><a href="#添加连接" class="headerlink" title="添加连接"></a>添加连接</h4><p>HikariPool 实现了 IBagStateListener 接口，当连接不足时，会调用 <code>addBagItem</code> 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addBagItem</span><span class="params">(<span class="keyword">final</span> <span class="keyword">int</span> waiting)</span></span></span><br><span class="line"><span class="function"></span>&#123;   <span class="comment">// 检查应该添加的数量</span></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">boolean</span> shouldAdd = waiting - addConnectionQueue.size() &gt;= <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (shouldAdd) &#123;</span><br><span class="line">        <span class="comment">// 向线程池提交添加任务</span></span><br><span class="line">        addConnectionExecutor.submit(poolEntryCreator);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>addBagItem 方法比较简单，主要看一下任务的实现</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">PoolEntryCreator</span> <span class="keyword">implements</span> <span class="title">Callable</span>&lt;<span class="title">Boolean</span>&gt;</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> String loggingPrefix;</span><br><span class="line"></span><br><span class="line">    PoolEntryCreator(String loggingPrefix)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">this</span>.loggingPrefix = loggingPrefix;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Boolean <span class="title">call</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> sleepBackoff = <span class="number">250L</span>;</span><br><span class="line">        <span class="keyword">while</span> (poolState == POOL_NORMAL &amp;&amp; shouldCreateAnotherConnection()) &#123;</span><br><span class="line">            <span class="comment">// 创建一个连接</span></span><br><span class="line">            <span class="keyword">final</span> PoolEntry poolEntry = createPoolEntry();</span><br><span class="line">            <span class="keyword">if</span> (poolEntry != <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="comment">// 添加到并发管理</span></span><br><span class="line">                connectionBag.add(poolEntry);</span><br><span class="line">                logger.debug(<span class="string">"&#123;&#125; - Added connection &#123;&#125;"</span>, poolName, poolEntry.connection);</span><br><span class="line">                <span class="keyword">if</span> (loggingPrefix != <span class="keyword">null</span>) &#123;</span><br><span class="line">                logPoolState(loggingPrefix);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> Boolean.TRUE;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 创建失败了，准备重试</span></span><br><span class="line">            quietlySleep(sleepBackoff);</span><br><span class="line">            sleepBackoff = Math.min(SECONDS.toMillis(<span class="number">10</span>), Math.min(connectionTimeout, (<span class="keyword">long</span>) (sleepBackoff * <span class="number">1.5</span>)));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> Boolean.FALSE;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 同步检查是否过载</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">synchronized</span> <span class="keyword">boolean</span> <span class="title">shouldCreateAnotherConnection</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getTotalConnections() &lt; config.getMaximumPoolSize() &amp;&amp;</span><br><span class="line">            (connectionBag.getWaitingThreadCount() &gt; <span class="number">0</span> || getIdleConnections() &lt; config.getMinimumIdle());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>还剩下一些零散的代码就不贴了，作者的思路还是挺好的，代码也不复杂，最关键还是上一篇的并发管理，连接泄露处理方式值得借鉴，有兴趣还是把源码下载下来看看。</p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/源码分析/" rel="tag"># 源码分析</a>
          
            <a href="/tags/HikariCP/" rel="tag"># HikariCP</a>
          
            <a href="/tags/并发/" rel="tag"># 并发</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/03/19/HikariCP源码深度解读（一）/" rel="next" title="HikariCP源码深度解读（一）">
                <i class="fa fa-chevron-left"></i> HikariCP源码深度解读（一）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="isunimp">
            
              <p class="site-author-name" itemprop="name">isunimp</p>
              <p class="site-description motion-element" itemprop="description">涉猎广泛，不亦乐乎，不求甚解。</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">3</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">2</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">5</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/isunimp" title="GitHub &rarr; https://github.com/isunimp" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:ansheck@163.com" title="E-Mail &rarr; mailto:ansheck@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#HikariDataSource"><span class="nav-number">1.</span> <span class="nav-text">HikariDataSource</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HikariPool"><span class="nav-number">2.</span> <span class="nav-text">HikariPool</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#获取连接"><span class="nav-number">2.1.</span> <span class="nav-text">获取连接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#关闭连接"><span class="nav-number">2.2.</span> <span class="nav-text">关闭连接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#添加连接"><span class="nav-number">2.3.</span> <span class="nav-text">添加连接</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2017 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">isunimp</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> v6.6.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script src="/js/src/utils.js?v=6.6.0"></script>

  <script src="/js/src/motion.js?v=6.6.0"></script>



  
  

  
  <script src="/js/src/scrollspy.js?v=6.6.0"></script>
<script src="/js/src/post-details.js?v=6.6.0"></script>



  


  <script src="/js/src/bootstrap.js?v=6.6.0"></script>



  



  











  





  

  

  

  

  

  
  

  

  

  

  

  

  

</body>
</html>
